<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Validator;
use DB;
class SignController extends Controller
{
    /**
     * 显示注册页面`
     * @return [type] [description]
     */
    public function index()
    {
        return view('sign.index');
    }
    /**
     * 执行数据的插入
     */
    public function doSign(Request $request)
    {
            $this->validate($request, [
                'username' => 'required|regex:/^\w{6,20}$/',
                'password' => 'same:repassword|regex:/^\S{6,20}$/|required',
                'phone'=>'required|regex:/^\d{11}$/',
            ],[    
                'username.required'=>'对不起用户名必填',
                'username.unique'=>'用户名已经存在',
                'password.required' => '密码必填',
                'password.same' => '两次密码不一致',
                'password.same' => '两次密码不一致',
                'phone.required'=>'手机号必填',
                'phone.regex'=>'手机号格式不对',
            ]);
        /**
         * 获取数据
         */
        $tmp['username'] = $request->username;
        $tmp['password'] = bcrypt($request->password);
        $tmp['phone'] = $request->phone;
        $tmp['status'] = 1;
        /**
         * 插入数据库
         */
        $data = DB::table('users')->insert($tmp);
        if ($data) {
            return redirect('/login')->with('info','注册成功');
        }
    }
}
